package com.cskaoyan.market.controller;
import com.cskaoyan.market.bo.TopicEditorBo;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.MarketTopicService;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.*;

@RequestMapping("/admin/topic")
@RestController
public class AdminTopicController extends HttpServlet {
    @Autowired
    private MarketTopicService topicService;

    @GetMapping("/read")
    private Object read(String id) {

        Integer idInt = null;
        try {
            idInt = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            ResponseUtil.fail(404, "id不能为非正整数");
        }
        TopicEditorBo topicEditorBo = topicService.read(idInt);
        if (topicEditorBo != null)
            return ResponseUtil.ok(topicEditorBo);
        else
            return ResponseUtil.fail(404, "id不存在");
    }

    @GetMapping("/list")
    private Object list(String page, String limit, String sort, String order, String title, String subtitle) {

        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
            List<MarketTopic> marketTopicList = topicService.list(pageInt, limitInt, sort, order, title, subtitle);
            return ResponseUtil.okList(marketTopicList);
        } catch (NumberFormatException e) {
            return ResponseUtil.fail(404, "page和limit不能为非正整数");
        }
    }

    @PostMapping("/batch-delete")
    private Object batchDelete(@RequestBody Map<String, ArrayList<String>> map)  {
        ArrayList<String> ids = new ArrayList<String>();
        ids=map.get("ids");
        if (ids == null || ids.size() == 0) {
            return ResponseUtil.fail(404, "ids不能为空");
        }
        try {
            topicService.batchDelete(ids);
            return ResponseUtil.ok();
        } catch (Exception e) {
            System.out.println("e = " + e);
            return ResponseUtil.fail(404, "删除失败");
        }
    }
    @PostMapping("/delete")
    private Object delete(@RequestBody MarketTopic marketTopic)  {
        if (marketTopic == null || marketTopic.getId() == null) {
            return ResponseUtil.fail(404, "id不能为空");
        }try {
            topicService.delete(marketTopic.getId());
            return ResponseUtil.ok();
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(404, "删除失败");
        }

    }
    @PostMapping("/update")
    private Object update(@RequestBody MarketTopic marketTopic) throws IOException {
        if (marketTopic == null || marketTopic.getId() == null) {
            return ResponseUtil.fail(404, "id不能为空");
        }try {
            topicService.update(marketTopic);
            return ResponseUtil.ok();
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(404, "删除失败");
        }

    }
    @PostMapping("/create")
    private Object create(@RequestBody MarketTopic marketTopic) throws IOException {
        if (marketTopic == null || marketTopic.getId() == null) {
            return ResponseUtil.fail(404, "id不能为空");
        }try {
            topicService.create(marketTopic);
            return ResponseUtil.ok();
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(404, "创建失败");
        }

    }
}
